$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL +'users/applyvip/list',
        datatype: "json",
        colModel: [
            { label: '申请id', name: 'id', index: "id", width: 45, key: true,hidden:true },
            { label: '手机号码', name: 'mobilePhone'},
            { label: '姓名', name: 'realName'},
            { label: '审批人号码 ', name: 'relationTel'},
            { label: '审批人', name: 'relationName'},
            { label: '审批状态', name: 'relationStatus'},
            { label: '二级审批人号码', name: 'sellerTel'},
            { label: '二级审批人', name: 'sellerName'},
            { label: '二级审批审核状态', name: 'sellerStatus', width: 100 },
            { label: '申请时间', name: 'createTime'}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.result",
            page: "page.pageNo",
            total: "page.totalPages",
            records: "page.total"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#applyvip',
    data:{
        q:{
            userName: null,
            mobilePhone: null,
            startDate:null,
            endDate:null,
            status:""
        },
        showList: true,
        showBtn: false,
        title:null,
        refuseReason:false,
        statuslist:[],
        info:{
            id:null,
            realName:null,
            createTime:null,
            paymentTypeName:null,
            paymentAmount:null,
            voucher:null,
            verifyStatusName:null,
            paymentAccount:null,
            mobilePhone:null
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        freeze:function () {// 冻结
            var grid = $("#jqGrid");
            var userId = grid.getGridParam("selrow");
            if(!userId){
                alert("请选择一条记录");
                return ;
            }
            $.ajax({
                type: "POST",
                url: baseURL + "users/applyvip/refuseFinalVerify",
                contentType: "application/json",
                data: JSON.stringify({id:userId}),
                success: function(r){
                    if(r.code === 'success'){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.message);
                    }
                },
                error:function (r) {
                    alert(r.responseJSON.message);
                }
            });
        },
        unFreeze:function () {// 通过
            var grid = $("#jqGrid");
            var userId = grid.getGridParam("selrow");
            if(!userId){
                alert("请选择一条记录");
                return ;
            }
            $.ajax({
                type: "POST",
                url: baseURL + "users/applyvip/finalVerify",
                contentType: "application/json",
                data: JSON.stringify({id:userId}),
                success: function(r){
                    if(r.code === 'success'){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.message);
                    }
                },
                error:function (r) {
                    alert(r.responseJSON.message);
                }
            });
        },
        applyVipInfo: function(){       //申请详情
            var grid = $("#jqGrid");
            var id = grid.getGridParam("selrow");
            if(!id){
                alert("请选择一条记录");
                return ;
            }
            $.get(baseURL + "users/applyvip/info/"+id, function(r){
                vm.showList = false;
                vm.title = "VIP申请详细信息";
                vm.info = r.data;
                if (vm.info.verifyStatusCode==='wait_verify' || vm.info.verifyStatusCode==='first_verify_pass')  {
                    vm.showBtn = true;
                } else {
                    vm.showBtn = false;
                }
            });
        },
        // 详情中预览转账凭证图片
        showImg:function(){
            var imgUrl = vm.info.voucher
            var img = new Image();
            img.src = imgUrl;
            layer.open({
                type: 1,
                title: false,
                closeBtn:2 ,
                area: [img.width,img.height],
                skin: 'layui-layer-nobg', //没有背景色
                content: '<img src="'+imgUrl+'" style="max-height: 900px;max-height: 500px;">'
            })
        },
        // 初审
        firstVerify: function () {
            var id = vm.info.id;
            if(id == null){
                return ;
            }
            $.ajax({
                type: "POST",
                url: baseURL + "users/applyvip/firstVerify/"+id,
                contentType: "application/json",
                data: JSON.stringify(vm.info),
                success: function(r){
                    if(r.code === 'success'){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.message);
                    }
                },
                error:function (r) {
                    alert(r.responseJSON.message);
                }
            });
        },
        // 终审
        finalVerify: function () {
            var id = vm.info.id;
            if(id == null){
                return ;
            }
            $.ajax({
                type: "POST",
                url: baseURL + "users/applyvip/finalVerify/"+id,
                contentType: "application/json",
                data: {},
                success: function(r){
                    if(r.code === 'success'){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.message);
                    }
                },
                error:function (r) {
                    alert(r.responseJSON.message);
                }
            });
        },
        // 拒绝申请
        refuseVerify:function(){
            layer.open({
                type: 1,
                btn: ['提交','取消'], //按钮
                area: ['420px', '240px'], //宽高
                offset: '100px',
                content: $('#refuseReason'),
                yes:function (index, layero) {
                    if (isBlank(vm.info.refuseReason)){
                        showTips("请输入拒绝原因", '#refuseReason');
                        return;
                    }
                    $.ajax({
                        type: "POST",
                        url: baseURL + "users/applyvip/refuseFirstVerify",
                        contentType: "application/json",
                        data: JSON.stringify(vm.info),
                        success: function(r){
                            if(r.code === 'success'){
                                layer.close(index);
                                alert('操作成功', function(){
                                    vm.reload();
                                });
                            }else{
                                alert(r.message);
                            }
                        },
                        error:function (r) {
                            alert(r.responseJSON.message);
                        }
                    });
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'realName': vm.q.userName,'mobilePhone': vm.q.mobilePhone,'startDate':vm.q.startDate,'endDate':vm.q.endDate,'status':vm.q.status},
                page:page
            }).trigger("reloadGrid");
        }
    },
    created:function(){
        $.get(baseURL + "users/applyvip/getApplyStatus", function(r){vm.statuslist = r.data;});
    }
});
layui.use('laydate', function(){
    var laydate = layui.laydate;
    laydate.render({
        elem: '#startDate',
        format:'yyyy-MM-dd HH:mm:ss',
        done: function(value, date, endDate){
            vm.q.startDate = value;
        }
    });
    laydate.render({
        elem: '#endDate',
        format:'yyyy-MM-dd HH:mm:ss',
        done: function(value, date, endDate){
            vm.q.endDate = value;
        }
    });
});
